package com.activiti.tool;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * 流程资源下载
 * @author ldanniel
 */
public class ProcessResourceDownLoad {
    public static void main(String[] args) {
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //通过流程定义信息获取部署id
        ProcessDefinition definition = repositoryService
                .createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();

        String deploymentId = definition.getDeploymentId();
        //act_re_procdef表中获取资源名称
        String resourceName = definition.getResourceName();
        //通过repositoryService 传递部署id参数，读取资源

        InputStream bpmnResouce = repositoryService.getResourceAsStream(deploymentId, resourceName);
        File file = new File("D:\\workspace\\activiti7\\download\\evection.bpmn");
        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;
        try {
             inputStream = new BufferedInputStream(bpmnResouce);
             outputStream = new BufferedOutputStream(new FileOutputStream(file));
            //转换输入和输出流
            int copy = IOUtils.copy(inputStream, outputStream);
            if(copy > 0){
                System.out.println("下载资源成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                assert inputStream != null;
                inputStream.close();
                assert outputStream != null;
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
